layui.use(['form', 'table','miniPage','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        miniPage = layui.miniPage;

    var tableIns = table.render({
        elem: '#currentTableId',
        url: 'configInfo/list',
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'id', width: 80, title: 'ID', sort: true},
            {field: 'name', title: '配置名称'},
            {field: 'configKey',  title: '配置key', sort: true},
            {field: 'configValue', title: '配置的value'},
            {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        table.reload('currentTableId',{
            where: data.field
        } , true);
        return false;
    });

    /**
     * toolbar事件监听
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {   // 监听添加操作
            var content = miniPage.getHrefContent('page/business/configInfoAdd.html');
            var openWH = miniPage.getOpenWidthHeight();

            var index = layer.open({
                title: '增加车辆信息',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0]/2 + 'px', openWH[1] + 'px'],
                offset:'auto',
                content: content,
                end:function(){
                    tableIns.reload()
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });
    var parentIndex;
    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {

            var content = miniPage.getHrefContent('page/business/configInfoEdit.html');
            var openWH = miniPage.getOpenWidthHeight();

            var index = layer.open({
                title: '编辑',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: [openWH[0]/2 + 'px', openWH[1] + 'px'],
                content: content,
                success:function(layero, index){
                    form.val("formTest",data);
                },
                end:function(){
                    tableIns.reload()
                }
            });
            parentIndex = index;
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });
    //时间格式化的函数
    form.on('submit(saveBtn)', function (data) {
        if (data.field.id != undefined && data.field.id !=null && data.field.id != ''){
            $.ajax({
                type: "POST",
                url: "configInfo/update",//发送请求
                data: data.field,
                // contentType:"application/json;charset=utf-8",
                success: function (result) {
                    if (result.code == 0){
                        layer.msg(result.msg)
                    }else {
                        layer.msg(result.msg)
                    }
                },
                error: function (err) {
                    console.error(err)
                },
                complete: function (res) {
                    // 关闭弹出层
                    // layer.close(index);
                    layer.close(parentIndex);
                }
            })
        }

        return false;
    });
});
